package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.FlashPromotionProductRelation;
import com.itheima.domain.Product;
import com.itheima.mapper.FlashPromotionProductRelationMapper;
import com.itheima.mapper.ProductMapper;
import com.itheima.service.SmsFlashPromotionProductRelationService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsFlashPromotionProductRelationServiceImpl implements SmsFlashPromotionProductRelationService {

    @Autowired
    FlashPromotionProductRelationMapper relationMapper;
    @Autowired
    ProductMapper pmsProductMapper;

    //分页查询不同场次关联及商品信息
    @Override
    public PageBeanVo list(Integer pageNum, Integer pageSize, Integer flashPromotionId, Integer flashPromotionSessionId) {
        Page<FlashPromotionProductRelation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<FlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FlashPromotionProductRelation::getFlashPromotionId, flashPromotionId)
                .eq(FlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId)
                .orderByAsc(FlashPromotionProductRelation::getSort);
        Page<FlashPromotionProductRelation> selectPage = relationMapper.selectPage(page, lqw);

        return new PageBeanVo(pageNum, pageSize, selectPage.getTotal(), selectPage.getRecords());
    }

    //根据id查询
    @Override
    public Product getProductById(Long id) {
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getId, id);
        Product pmsProduct = pmsProductMapper.selectOne(lqw);
        return pmsProduct;
    }

    //批量选择商品添加关联
    @Override
    public void create(FlashPromotionProductRelation relation) {
        relationMapper.insert(relation);
    }
    //删除关联

    @Override
    public void delete(Integer id) {
        relationMapper.deleteById(id);
    }

    //修改关联信息
    @Override
    public void update(FlashPromotionProductRelation relation) {
        relationMapper.updateById(relation);
    }

    //获取管理商品促销信息
    @Override
    public FlashPromotionProductRelation selectById(Integer id) {
        LambdaQueryWrapper<FlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FlashPromotionProductRelation::getId, id);
        FlashPromotionProductRelation relation = relationMapper.selectOne(lqw);
        return relation;
    }
}
